package coder.common;

import coder.exception.BaseException;
import coder.exception.UnauthorizedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice
@Slf4j
@RestController
public class GlobalExceptionHandler  {
    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception ex){
        log.error(ex.getMessage());
        if(ex instanceof BaseException) {
            return Result.error(ex.getMessage());
        } else if(ex instanceof UnauthorizedException){
           return Result.unauthorized(ex.getMessage());
        }
        return Result.notFound(ex.getMessage());
    }
}
